package net.zjitc.controller;

import net.zjitc.pojo.User;
import net.zjitc.service.UserService;
import net.zjitc.service.impl.UserServiceImpl;
import net.zjitc.utils.dispacher.URLMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by Aspire on 2017/12/3.
 */
public class AjaxFindUserName {
    @URLMapping(url = "/findUsername")
    public String findUsername(HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("username");
        UserService userService = new UserServiceImpl();
        int result = userService.findUsername(username);
        try{
            if (result > 0){
                response.getWriter().print(true);
            }else {
                response.getWriter().print(false);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    @URLMapping(url = "/getState")
    public String getState(HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        UserService userService = new UserServiceImpl();
        User user = userService.getState(username, password);
        try{
            if (user != null && user.getState() == 1){
                request.getSession().setAttribute("user", user);
                response.getWriter().print("1");
            }else {
                response.setContentType("text/html;charset=utf-8");
                response.getWriter().print("用户名或密码错误，或者您还未激活");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
}
